package com.zzq.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.zzq.domain.dto.SmilePointGoodsDTO;
import com.zzq.domain.entity.SmileCouponGoodsRel;
import com.zzq.domain.entity.SmileCouponRule;
import com.zzq.domain.entity.SmilePointGoods;
import com.zzq.domain.search.PointGoodsSearchParamet;
import com.zzq.enumerate.GoodsTypeEnum;
import com.zzq.enumerate.OssPathEnum;
import com.zzq.result.SmileResult;
import com.zzq.service.PointGoodsAdminService;
import com.zzq.service.SmileCouponGoodsRelService;
import com.zzq.service.SmileCouponRuleService;
import com.zzq.service.SmilePointGoodsService;
import com.zzq.util.ObjectUtils;
import com.zzq.util.OssFileUtil;
import com.zzq.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class PointGoodsAdminServiceImpl implements PointGoodsAdminService {

    @Autowired
    SmilePointGoodsService pointGoodsService;
    @Autowired
    SmileCouponGoodsRelService couponGoodsRelService;
    @Autowired
    SmileCouponRuleService couponRuleService;

    @Autowired
    OssFileUtil ossFileUtil;

    @Override
    public SmileResult getPointGoodsDTOList(PointGoodsSearchParamet searchParamet) {
        List<SmilePointGoodsDTO> pointGoodsDTOList = pointGoodsService.getPointGoodsDTOList(searchParamet);
        return SmileResult.sucess(pointGoodsDTOList);
    }

    @Override
    @Transactional
    public SmileResult add(MultipartFile goodsCoverFile, Long couponRuleId, SmilePointGoods pointGoods) throws IOException, ClientException {
        pointGoodsService.save(pointGoods);
        Long goodsId = pointGoods.getId();
        if (!ObjectUtils.isEmpty(goodsCoverFile)) {
            String path = ossFileUtil.uploadStaticFile(goodsCoverFile, OssPathEnum.GOODS_RESOURCES.getPath(goodsId+""), UUIDUtil.getUUID());
            String staticFileUrl = ossFileUtil.getStaticFileUrl(path);
            pointGoods.setGoodsCoverUrl(staticFileUrl);
        }


        if (GoodsTypeEnum.COUPON.getName().equals(pointGoods.getGoodsType())){
            SmileCouponRule couponRule = couponRuleService.getById(couponRuleId);
            pointGoods.setGoodsDescription("满"+couponRule.getAmountFull().toString()+"元可用");
            SmileCouponGoodsRel couponGoodsRel=new SmileCouponGoodsRel();
            couponGoodsRel.setGoodsId(goodsId);
            couponGoodsRel.setCouponRuleId(couponRuleId);
            couponGoodsRelService.save(couponGoodsRel);
        }
        pointGoodsService.updateById(pointGoods);

        return SmileResult.sucess();
    }

    @Override
    @Transactional
    public SmileResult upd(MultipartFile goodsCoverFile, SmilePointGoods pointGoods) throws IOException, ClientException {
        Long goodsId = pointGoods.getId();
        if (!ObjectUtils.isEmpty(goodsCoverFile)) {
            String path = ossFileUtil.uploadStaticFile(goodsCoverFile, OssPathEnum.GOODS_RESOURCES.getPath(goodsId+""), UUIDUtil.getUUID());
            String staticFileUrl = ossFileUtil.getStaticFileUrl(path);
            pointGoods.setGoodsCoverUrl(staticFileUrl);
        }
        pointGoodsService.updateById(pointGoods);
        return SmileResult.sucess();
    }

    @Override
    @Transactional
    public SmileResult del(Long goodsId)  {
        pointGoodsService.removeById(goodsId);
        return SmileResult.sucess();
    }
}
